// $Id: rpsutils.jsm 7 2012-02-28 12:50:59Z hhofer69@gmail.com $

"use strict";

Components.utils.import("resource://gre/modules/Services.jsm");
Components.utils.import("resource://gre/modules/FileUtils.jsm");
Components.utils.import("resource://gre/modules/DownloadLastDir.jsm");  
Components.utils.import("resource://services-sync/main.js");
Components.utils.import("chrome://rpsmodules/content/common/syncutils.jsm");
Components.utils.import("chrome://rpsmodules/content/common/logutils.jsm");

var EXPORTED_SYMBOLS = [ "RequestPolicyBackup" ];


//*****************************************************************************
//* Backup / Restore
//*****************************************************************************

const assert = SyncUtil.assert;

var RequestPolicyBackup = {
  OK:        0,
  CANCELLED: 1,
  FAILED:    2,
  
  bakdir: FileUtils.getDir("ProfD", ["reqpolsync"]),
  
  backup: function STB_backup(sts, file) {
    try {
      let ReqPol = {}
      Components.utils.import("resource://requestpolicy/FileUtil.jsm", ReqPol);
      Components.utils.import("chrome://reqpolsync/content/rpsengine.jsm");

      if (!file) {
        let  maxAge = Services.prefs.getIntPref("extensions.reqpolsync.bakage") * 24 * 3600 * 1000;
        file = SyncUtil.makeBackupFile(this.bakdir, "reqpolsync", ".txt", maxAge);
        if (file.exists()) return this.CANCELLED;
      }
      if (file) {
        if (!file.parent.exists())  file.parent.create(1, 0x700);
        if (file.exists())          file.remove(false);

        // behave like prefWindow.js, but use our cache
        ReqPolCache.update();
        let cached = ReqPolCache.items(), types = ReqPolCache.POLICY_TYPES;
        let lines  = [];
        for (let t in types) { // orig, dest, o2d
          lines.push("["+types[t].section+"]");
          Object.keys(cached)
                .filter( function(guid) { return cached[guid].type == t; })
                .forEach(function(guid) { lines.push(cached[guid].value); });
        }
        ReqPol.FileUtil.arrayToFile(lines, file);
        
        Logging.debug("created backup: "+file.path);
        return this.OK;
      }
    } catch (exc) {
      Logging.logException(exc);
    }
    return this.FAILED;
  },
  
  restore: function STB_restore(sts, file) {
    return this.FAILED; // use RequestPolicy's import
  },
  
  firstStart: function STB_firstStart(sts, doBackup) {
    gDownloadLastDir.setFile("chrome://reqpolsync", this.bakdir);
    if (!this.bakdir.exists())
      this.bakdir.create(1, 0x700);
    if (doBackup) {
      let f = this.bakdir.clone();
      f.append("reqpolsync-firstrun.txt");
      this.backup(sts, f);
    }
  },
};

//*****************************************************************************
//* Logging
//*****************************************************************************
Logging.CATEGORY     = "reqpolsync";
Logging.PFX          = Logging.CATEGORY+": ";
Logging.DEBUG        = Services.prefs.getBoolPref("extensions.reqpolsync.debug");
Logging.LOGFILE_PREF = "extensions.reqpolsync.logfile";

SyncUtil.setLogging(Logging);
